package io.babystar.modules.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.R;
import io.babystar.common.utils.ShiroUtils;
import io.babystar.modules.business.entity.CWRefundEntity;
import io.babystar.modules.business.entity.StudentEntity;
import io.babystar.modules.business.service.CWCategoryService;
import io.babystar.modules.business.service.CWOrderService;
import io.babystar.modules.business.service.CWRefundService;
import io.babystar.modules.business.service.StudentService;
import io.babystar.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("business/cwRefund")
public class CWRefundController {
    @Autowired
    private CWRefundService cwRefundService;
    @Autowired
    private CWCategoryService cwCategoryService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private CWOrderService cwOrderService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:cwRefund:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = cwRefundService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("business:cwRefund:info")
    public R info(@PathVariable("id") Long id){
        CWRefundEntity cwRefund = cwRefundService.getById(id);
        if(null != cwRefund){
            cwRefund.setCategoryName(cwCategoryService.getById(cwRefund.getCategoryId()).getCategory());
            //cwRefund.setStudentName(studentService.getById(cwRefund.getStudentId()).getStudentName());
            //cwRefund.setAmountPaid(cwOrderService.getById(cwRefund.getOrderId()).getAmountPaid());
        }

        return R.ok().put("cwRefund", cwRefund);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("business:cwRefund:save")
    public R save(@RequestBody CWRefundEntity cwRefund){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        cwRefund.setCreator(userEntity.getUserId().toString());
        cwRefund.setCreateAt(new Date());
        cwRefundService.save(cwRefund);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("business:cwRefund:update")
    public R update(@RequestBody CWRefundEntity cwRefund){
        SysUserEntity userEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        cwRefund.setUpdator(userEntity.getUserId().toString());
        cwRefund.setUpdateAt(new Date());
        cwRefundService.updateById(cwRefund);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("business:cwRefund:delete")
    public R delete(@RequestBody Long[] ids){
        cwRefundService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
